var provinces = new Array("湘","京","沪","浙","苏","粤","鲁","晋","冀",
            "豫","川","渝","辽","吉","黑","皖","鄂",
            "津","贵","云","桂","琼","青","新","藏",
            "蒙","宁","甘","陕","闽","赣");

var keyNums = new Array("0","1","2","3","4","5","6","7","8","9",
            "Q","W","E","R","T","Y","U","P",
            "A","S","D","F","G","H","J","K","L",
            "Z","X","C","V","B","N","M");
var next = 0;
var lastNext = 0;
	var isCoaches = false;//教练车开关

$(function () {
	$(".xtc__item_car").click(function () {
		var imgSrcYes="";
		var imgSrcNo="";
		var type = $(this).attr("data-type");
		if(type == 1){//为能源车的时候
			if($(this).find("img").attr("src") == imgSrcYes){
				$(this).find("img").attr("src", imgSrcNo);
				$(".content-car-number div").last().css("display", "none");
				$(".content-car-number div").last().find("span").text("");
				$(".content-car-number div").last().find("span").removeClass("have-text");
				$(".content-car-number div").last().removeClass("ppHas");
				if (next == 7) {
					next = next -1;
				}
				console.log(next)
			}else {
				$(".xtc__item_car img").attr("src", imgSrcNo);
				$(this).find("img").attr("src", imgSrcYes);
				$(".content-car-number div").last().css("display", "block");
				if (isCoaches){
					$(".content-car-number div").eq(6).find("span").text("");
					$(".content-car-number div").eq(6).find("span").removeClass("have-text");
					isCoaches = false;
				}
			}
		}
		if(type == 2){//为教练车的时候
			if($(this).find("img").attr("src") == imgSrcYes){
				$(this).find("img").attr("src", imgSrcNo);
				$(".content-car-number div").eq(6).find("span").text("");
				$(".content-car-number div").eq(6).find("span").removeClass("have-text");
				$(".content-car-number div").eq(6).removeClass("ppHas");
				isCoaches = false;
			}else {
				$(".xtc__item_car img").attr("src", imgSrcNo);
				$(this).find("img").attr("src", imgSrcYes);
				$(".content-car-number div").eq(6).find("span").text("学");
				$(".content-car-number div").eq(6).find("span").addClass("have-text");
				$(".content-car-number div").eq(6).removeClass("ppHas");
				isCoaches = true;

				$(".content-car-number div").last().css("display", "none");
				$(".content-car-number div").last().find("span").text("");
				$(".content-car-number div").last().find("span").removeClass("have-text");
				if (next == 7) {
					next = next -2;
				}
				if (next == 6) {
					next = next -1;
				}
			}
		}

	});
});
	function showProvince(){
			$("#pro").html("");
			var ss="";
			for(var i=0;i<provinces.length;i++){
				ss=ss+addKeyProvince(i);
			} 
			$("#pro").html("<ul class='clearfix ul_pro'>"+ss+"<li class='li_close' onclick='closePro();'><span>关闭</span></li><li class='li_clean' onclick='cleanPro();'><span>清空</span></li></ul>");
	}

	function showKeybord(){
			$("#pro").html("");
			var sss="";
			var i;
			for(i=0;i<keyNums.length;i++){
				sss=sss+'<li class="ikey ikey'+i+' '+(i>9?"li_zm":"li_num")+' '+(i>28?"li_w":"")+'" ><span onclick="choosekey(this,'+i+');">'+keyNums[i]+'</span></li>';
			} 
			$("#pro").html("<ul class='clearfix ul_keybord'>"+sss+"<li class='li_clean' onclick='choosekey(this, "+(i+1)+");'><span>删除</span></li><li class='li_clean' onclick='choosekey(this, "+(i)+");'><span>确定</span></li></ul>");
	}
	
    function addKeyProvince(provinceIds){
        var addHtml = '<li>';
            addHtml += '<span onclick="chooseProvince(this);">'+provinces[provinceIds]+'</span>';
            addHtml += '</li>';
            return addHtml;
    }

    function chooseProvince(obj){
       $(".input_pro span").text($(obj).text());
	   $(".input_pro").addClass("hasPro");
	   $(".input_pro").find("span").addClass("have-text");
	   showKeybord();
	   getpai();
	}	
	
	
	function choosekey(obj,jj){	
		if(jj==34){
			var carNo = $(".car_input").attr("data-pai");

			if ($(".content-car-number div").last().css("display") == "none"  || carNo.length < 7) {
				layer.open({
					content: "普通车牌为7位！"
					,skin: 'msg'
					,time: 2 //2秒后自动关闭
				});
				return;
			}

			if (carNo.length < 8 && $(".content-car-number div").last().css("display") != "none" ) {
				layer.open({
					content: "新能源车牌为8位！"
					,skin: 'msg'
					,time: 2 //2秒后自动关闭
				});
				return;
			}
			layer.closeAll();
			return ;
		}else if(jj==35){
			if($(".ppHas").length==0){
				$(".hasPro").find("span").text("");			
				$(".hasPro").find("span").removeClass("have-text");	
				$(".hasPro").removeClass("hasPro");	
				showProvince();
				next=0;
				return;
			}

			$(".ppHas:last").find("span").text("");	
			$(".ppHas:last").find("span").removeClass("have-text");	
			$(".ppHas:last").removeClass("ppHas");	
			next=next-1;
			if(next<1){
				next=0;
			}
			getpai();
			return;
		}else{
			console.log($(".content-car-number div").last().css("display"))
			if (next == 6 & $(".content-car-number div").last().css("display") == "none") {
				return;
			}
			for(var i = 0; i<$(".input_pp").length;i++){
				if (next == 5 && isCoaches ){
					return;
				}
				if(next==0 & jj<10 & $(".input_pp:eq("+next+")").hasClass("input_zim")){
					layer.open({
						content: "车牌的第二位必须是字母"
						,skin: 'msg'
						,time: 2 //2秒后自动关闭
					});
					return
				}
				$(".input_pp:eq("+next+")").find("span").text($(obj).text());
				$(".input_pp:eq("+next+")").addClass("ppHas");
				$(".input_pp:eq("+next+")").find("span").addClass("have-text");


				if(lastNext == 0){
					next=next+1;
					if(next>6){
						next=7;
					}
				}else {
					next = lastNext;
					lastNext = 0;
				}

				console.log(next);
				getpai();
				return
			}
		}
	}
	
	function closePro(){
       layer.closeAll();
	}		
	
	function cleanPro(){
       $(".content-car-number").find("span").text("");
       $(".hasPro").find("span").removeClass("have-text");
       $(".ppHas").find("span").removeClass("have-text");
       $(".hasPro").removeClass("hasPro");
       $(".ppHas").removeClass("ppHas");
	   next=0;
	}	
	
	function getpai(){
		var pai=$(".car_input span").text();
		$(".car_input").attr("data-pai",pai);
	}
	
	function genCarNoInput() {
		var carIpthtml = "<li class='input_pp'><span></span></li>";
		return carIpthtml;
	}
	
	
window.onload = function() {
	$(".input_pro").click(function(){
		$(this).find("span").removeClass("have-text");
		 layer.open({
				type: 1
				,content: '<div id="pro"></div>'
				,anim: 'up'
				,shade :false 
				,style: ' bottom:0; left:0; width: 100%; height: auto; padding:0; border:none;box-shadow:none'
			  });
		 showProvince();
	});


	$(".input_pp").click(function(){
		 if($(".input_pro").hasClass("hasPro")){ // 如果已选择省份
		 	lastNext = next;
		 	console.log("nextIndex : " + $(this).attr("nextIndex"))
		 	next = $(this).attr("nextIndex");
		 	$(this).find("span").removeClass("have-text");
		 	 layer.open({
					type: 1
					,content: '<div id="pro"></div>'
					,anim: 'up'
					,shade :false 
					,style: ' bottom:0; left:0; width: 100%; height: auto; padding:0; border:none;box-shadow:none'
				  });
			 showKeybord();
		 }else{
			 $(".input_pro").click();
		 }
	});
};
